const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const cors = require('koa2-cors');
const { connectDb, loadSchema } = require('./databse/init');

//#endregion
const app = new Koa();
const childrenRouter = new Router();
connectDb();
loadSchema();

// 加载路由模块
const UserRouter = require('./api/user');
const ArticleRouter = require('./api/article');

app.use(cors({
  origin: 'http://localhost:8080',
  credentials: true,
}));

app.use(bodyParser());

childrenRouter.use('/shareVue/user', UserRouter.routes());
childrenRouter.use('/shareVue/article', ArticleRouter.routes());
// app的中间件前面不可以写路由，只能是个函数
app.use(childrenRouter.routes()).use(childrenRouter.allowedMethods());
app.listen(8088, () => {
  console.log('服务运行中...');
})